package thread;

class ThreadMy01 implements Runnable{
    @Override
    public void run() {
            String name = Thread.currentThread().getName();
        while (true){
            System.out.println(name+"->MyThread01.run()");
        }
    }
}
class ThreadMy02 implements Runnable{
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        while (true){
            System.out.println(name+"->MyThread02.run()");
        }
    }
}


/**
 * @Author Justin Parker
 * @Date 2024/7/19 0019 14:35
 * @Version 1.0
 */
public class ThreadDemoo02 {

    public static void main(String[] args) {
        //获取main方法运行在哪个线程，这个线程名字是什么
        System.out.println(Thread.currentThread().getName());
        //创建两个新的任务对象(类型为Runnable类型)
         ThreadMy01 task1=new ThreadMy01();
         ThreadMy02 task2=new ThreadMy02();
        //创建两个新的线程对象，并将任务交给线程对象
        Thread t1=new Thread(task1);
        Thread t2=new Thread(task2);
        //启动线程对象
        t1.start();//线程启动后获取了CPU会执行自己的run方法
        t2.start();
        //要求：参考34行代码，用lambda表达式方式实现线程对象创建
        Thread t3=new Thread(()->{
            String name = Thread.currentThread().getName();
            while (true){
                System.out.println(name+"->MyThread03.run()");
            }
        });
        t3.start();

        //要求：参考34行代码，用匿名内部类方式实现线程对象创建
        Thread t4=new Thread(new Runnable() {
            @Override
            public void run() {
                String name = Thread.currentThread().getName();
                while (true){
                    System.out.println(name+"->MyThread04.run()");
                }
            }
        });
        t4.start();
    }
}
